<?php
defined('BASEPATH') or exit('No direct script access allowed');

use QCloud_WeApp_SDK\Auth\LoginService as LoginService;
use QCloud_WeApp_SDK\Constants as Constants;

class Login extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->model('Customer_model');
    }

    /**
     * @SWG\Get(
     *     summary="用户授权登录",
     *     path="/login",
     *     tags={"微信授权登录"},
     *     operationId="4",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="用户类型",
     *         in="query",
     *         name="type",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function index()
    {
        $get = $this->input->get();
        // 微信授权登录
        if (!isset($get['type'])) {
            send_json([
                'code'  => -1,
                'error' => '缺少类型参数',
            ]);

            if ($get['type'] != 'customer' && $get['type'] != 'merchants') {
                send_json([
                    'code'  => 4000,
                    'error' => '类型参数错误',
                ]);
            }
        }
        $result = LoginService::login();
        
        if ($result['loginState'] === Constants::S_AUTH) {
            // 返回角色类型 和 ID
            if (!isset($result['userinfo']['userinfo']->uid) && !$result['userinfo']['userinfo']->uid) {
                $this->json([
                    'code'  => 4004,
                    'error' => '没有该用户',
                ]);
            }
            $rk = LoginService::checkType($result['userinfo']['userinfo']->uid, $get['type']);
            if (!$rk) {
                $this->json([
                    'code'  => 4004,
                    'error' => '没有该用户',
                ]);
            }
            $result['userinfo']['userinfo']->type = $rk['type'];
            if($get['type'] == 'customer'){
                $row = $this->Customer_model->isBinding($result['userinfo']['sessionKey']);
                if ($row) {
                    $result['userinfo']['isbinding'] = 1;
                    $result['userinfo']['userinfo']->isbinding = 1;
                } else {
                    $result['userinfo']['isbinding'] = 0;
                    $result['userinfo']['userinfo']->isbinding = 0;
                }
            }
            log_message('error', '登录日志' . json_encode($result));
            $this->json([
                'code' => 2000,
                'data' => $result['userinfo'],
            ]);
        } else {
            $this->json([
                'code'  => 4004,
                'error' => $result['error'],
            ]);
        }
    }
}
